import mariadb


class MariadbClient:
    def __init__(self, host, port , user, password, db, poolname, pool_size):
        self.pool = mariadb.ConnectionPool(database=db,host=host,port=port,user=user,password=password,pool_name=poolname,pool_size=pool_size)

    def get_conn(self):
        return self.pool.get_connection()

    def execute(self, sql, values=None):
        with self.get_conn() as conn:
            with conn.cursor() as cur:
                if values:
                    cur.execute(sql, values)
                else:
                    cur.execute(sql)
                conn.commit()

    def query(self, sql, values=None):
        with self.get_conn() as conn:
            with conn.cursor() as cur:
                if values:
                    cur.execute(sql, values)
                else:
                    cur.execute(sql)
                rows = cur.fetchall()
                return rows

    def insert(self, table, params , value):
        sql = "INSERT INTO {} ({}) VALUES ({})".format(table, params, value)
        #print(sql)
        self.execute(sql)


    def delete(self, table, where):
        sql = "DELETE FROM {} WHERE {}".format(table, where)
        self.execute(sql)


    def update(self, table, params, where):
        sql = "UPDATE {} SET {} WHERE {}".format(table, params, where)
        #print(sql)
        self.execute(sql)